﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlackBBS.Models.Data;

namespace BlackBBS.Infrastructure.Repository
{
    public class SiderServerRepository : IServerRepository
    {
        public IRepository<BBSListEntry> BBSLists { get; set; }
        public IRepository<CallHistory> CallHistories { get; set; }
        public IRepository<ChatRoom> ChatRooms { get; set; }
        public IRepository<Email> Emails { get; set; }
        public IRepository<ExtProgram> ExtPrograms { get; set; }
        public IRepository<File> Files { get; set; }
        public IRepository<FileArea> FileAreas { get; set; }
        public IRepository<Group> Groups { get; set; }
        public IRepository<MessageArea> MessageAreas { get; set; }
        public IRepository<Message> Messages { get; set; }
        public IRepository<Network> Networks { get; set; }
        public IRepository<Session> Sessions { get; set; }
        public IRepository<User> Users { get; set; }

        public SiderServerRepository()
        {
        }

        public void Init(ConfigSettings settings)
        {
            this.BBSLists = new SiderRepository<BBSListEntry>("urn:bbslist", settings.DBHostname);
            this.CallHistories = new SiderRepository<CallHistory>("urn:callhistories", settings.DBHostname);
            this.ChatRooms = new SiderRepository<ChatRoom>("urn:chatrooms", settings.DBHostname);
            this.Emails = new SiderRepository<Email>("urn:emails", settings.DBHostname);
            this.ExtPrograms = new SiderRepository<ExtProgram>("urn:extprograms", settings.DBHostname);
            this.Files = new SiderRepository<File>("urn:files", settings.DBHostname);
            this.FileAreas = new SiderRepository<FileArea>("urn:fileareas", settings.DBHostname);
            this.Groups = new SiderRepository<Group>("urn:groups", settings.DBHostname);
            this.MessageAreas = new SiderRepository<MessageArea>("urn:messageareas", settings.DBHostname);
            this.Messages = new SiderRepository<Message>("urn:messages", settings.DBHostname);
            this.Networks = new SiderRepository<Network>("urn:networks", settings.DBHostname);
            this.Sessions = new SiderRepository<Session>("urn:sessions", settings.DBHostname);
            this.Users = new SiderRepository<User>("urn:users", settings.DBHostname);

        }

        public void Dispose()
        {
            this.BBSLists.Dispose();
            this.CallHistories.Dispose();
            this.ChatRooms.Dispose();
            this.Emails.Dispose();
            this.ExtPrograms.Dispose();
            this.Files.Dispose();
            this.FileAreas.Dispose();
            this.Groups.Dispose();
            this.MessageAreas.Dispose();
            this.Messages.Dispose();
            this.Networks.Dispose();
            this.Sessions.Dispose();
            this.Users.Dispose();
        }

        string BuildFilename<T>(string rootPath)
        {
            return System.IO.Path.Combine(rootPath, typeof(T).ToString().Replace(".", "_"));
        }
    }
}
